﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Microsoft.Practices.Unity;
using DreamBuilder.UI.UserControls.DataAccess;
using DreamBuilder.UI.UserControls.DataAccess.Services;
using DreamBuilder.Core.Container;

namespace DreamBuilder.UI.UserControls
{
    public partial class AttachmentViewer : ControlBase
    {
        public string ObjectID
        {
            set
            {
                ViewState["ObjectID"] = value;
            }
            get
            {
                if (ViewState["ObjectID"] == null)
                {
                    return String.Empty;
                }
                return (string)ViewState["ObjectID"];
            }
        }

        public string ObjectTableName
        {
            set
            {
                ViewState["ObjectTableName"] = value;
            }
            get
            {
                if (ViewState["ObjectTableName"] == null)
                {
                    return String.Empty;
                }
                return (string)ViewState["ObjectTableName"];
            }
        }

        public string AttachmentDBHelperString
        {
            get
            {
                return ((ViewState["DBHelperString"] == null) ? null : ViewState["DBHelperString"].ToString());
            }
            set
            {
                ViewState["DBHelperString"] = value;
            }
        }

        /// <summary>
        /// 附件类型//ADD By YSP 2011-07-25
        /// </summary>
        public string FunctionID
        {
            set
            {
                ViewState["FunctionID"] = value;
            }
            get
            {
                if (ViewState["FunctionID"] == null)
                {
                    return String.Empty;
                }
                return (string)ViewState["FunctionID"];
            }
        }
        
        public DataTable CurrDataTable
        {
            set
            {
                ViewState["CurrDataTable"] = value;
            }
            get
            {
                if (ViewState["CurrDataTable"] == null)
                {
                    ViewState["CurrDataTable"] = attachmentDAL.GetAttachmentList(this.ObjectID, this.ObjectTableName, "", "", this.FunctionID);
                }
                return (DataTable)ViewState["CurrDataTable"];
            }
        }

        [Dependency]
        public AttachmentDAL attachmentDAL
        {
            get
            {
                AttachmentDAL attachmentDAL = UContainer.Resolve<AttachmentDAL>();
                attachmentDAL.AttachmentDBHelperString = AttachmentDBHelperString;
                return attachmentDAL;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.ControlPath = "UserControls/AttachmentManager";
            if (!IsPostBack)
            {
                InitControlsCultureInfo();
                BindAttachmentListData();
            }
        }

        private void InitControlsCultureInfo()
        {
            this.litTitle.Text = GetString("attachment");
            this.GridView1.Columns[0].HeaderText = GetString("fileName");
            this.GridView1.Columns[1].HeaderText = GetString("fileSize");
            this.GridView1.Columns[2].HeaderText = GetString("fileDate");
        }
        private void BindAttachmentListData()
        {
            if (this.CurrDataTable != null && this.CurrDataTable.Rows.Count > 0)
            {
                this.GridView1.DataSource = CurrDataTable;
                this.GridView1.DataBind();
            }
            else
            {
                this.Visible = false;
            }
        }
    }
}